package com.redhat.redwork.widget.dropdown;

import java.util.Map;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;


/**
 * retrieve dropdown data from given data model
 * 
 * @author Justin Yip
 * 
 */
@Service
public class DropdownServiceImpl implements DropdownService,
		ApplicationContextAware {

	protected ApplicationContext applicationContext;

	/**
	 * applicationContext auto inject
	 */
	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		this.applicationContext = applicationContext;
	}

	public Map<String, String> loadDropdownData(String dataModel,
			String parentValue, Map<String, String> params) {

		if (dataModel == null)
			throw new IllegalArgumentException("datamodel must be defined");

		// get datamodel bean by name
		DropdownDataModel model = (DropdownDataModel) applicationContext
				.getBean(dataModel);

		return model.loadData(parentValue, params);
	}
}
